package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.R

/**
 * Created by DomenCai on 2022/3/24.
 */
class VideosAdapter : RecyclerView.Adapter<VideosAdapter.VideoViewHolder>() {

    private val videoList: MutableList<Video> = mutableListOf()

     @SuppressLint("NotifyDataSetChanged")
     fun updateList(newList: ArrayList<Video>) {
        videoList.clear()
        videoList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class VideoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val speechVideoName: TextView = view.findViewById(R.id.speechVideoName)
        val speechVideoHead: ImageView = view.findViewById(R.id.speechVideoTeacherHead)
        val speechVideoWatchNum: TextView = view.findViewById(R.id.watchNum)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_video, parent, false)
        return VideoViewHolder(view)
    }

    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
        val video = videoList[position]
        holder.speechVideoName.text = video.teacherName
        holder.speechVideoHead.setImageResource(video.teacherHead)
        holder.speechVideoWatchNum.text = video.watchNum
    }

    override fun getItemCount(): Int = videoList.size
}